package com.example.pt.mapper;

import com.example.pt.entity.CommentLike;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 评论点赞Mapper接口
 */
@Mapper
public interface CommentLikeMapper extends BaseMapper<CommentLike> {
    
    /**
     * 查询用户对评论的点赞状态
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 点赞记录
     */
    @Select("SELECT * FROM comment_like WHERE comment_id = #{commentId} AND user_id = #{userId} LIMIT 1")
    CommentLike selectByCommentIdAndUserId(@Param("commentId") Integer commentId, @Param("userId") Integer userId);
    
    /**
     * 取消点赞
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 影响行数
     */
    @Update("UPDATE comment_like SET status = 1 WHERE comment_id = #{commentId} AND user_id = #{userId}")
    int cancelLike(@Param("commentId") Integer commentId, @Param("userId") Integer userId);
    
    /**
     * 恢复点赞
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 影响行数
     */
    @Update("UPDATE comment_like SET status = 0 WHERE comment_id = #{commentId} AND user_id = #{userId}")
    int restoreLike(@Param("commentId") Integer commentId, @Param("userId") Integer userId);

    /**
     * 根据Commentid查询所有点赞的userid
     * @param commentId
     * @return userid的列表
     */
    @Select("SELECT user_id FROM comment_like WHERE comment_id = #{commentId} ORDER BY create_time DESC")
    List<Integer> getUserIdsByCommentId(@Param("commentId") Integer commentId);
} 